<template>
  <el-dialog width="600px" class="h500" :close-on-click-modal="false" :title="title" @close="clear" :visible.sync="visible" append-to-body>
    <el-form class="four" ref="form" :rules="rules" :model="formData" label-width="200px">
      <el-form-item label="工人姓名" label-width="150px" prop="workerId">
        <el-select v-model="formData.workerId" placeholder="姓名" filterable class="w150">
          <template v-for="item in this.userList">
            <el-option :label="item.userName" :value="item.id" :key="item.id"></el-option>
          </template>
        </el-select>
      </el-form-item>
      <el-form-item label="工作日期" label-width="150px" prop="taskDate">
        <el-date-picker v-model="formData.taskDate" type="date" placeholder="工作日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="开始时间" label-width="150px" prop="beginTime">
        <el-time-select placeholder="开始时间" v-model="formData.beginTime" />
      </el-form-item>
      <el-form-item label="结束时间" label-width="150px" prop="endTime">
        <el-time-select placeholder="结束时间" v-model="formData.endTime"  :picker-options="{minTime:formData.beginTime}"/>
      </el-form-item>
      <el-form-item label="完成任务" label-width="150px" prop="taskRemark">
        <el-input class="w300 cursorText" type="textarea" :row="5" v-model="formData.taskRemark"></el-input>
      </el-form-item>
    </el-form>
    <div class="footer">
      <el-button type="primary" @click="save">提交</el-button>
      <el-button @click="close">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script lang="ts">
import { Vue, Component } from 'vue-property-decorator'
import { State, namespace } from 'vuex-class'
import dayjs from 'dayjs'

const vuexAttendance = namespace('SixAttendance')
const vuexUser = namespace('User')

@Component({
  components: {}
})
export default class DialogDetail extends Vue {
  public $refs!: {
    form: any
  }
  @vuexUser.State userList: any
  @vuexAttendance.State workerList: any
  @vuexAttendance.Action ADD_ATTENDANCE: any
  title: string = '添加工作记录'
  visible: boolean = false

  formData: any = {
    workerId: '',
    beginTime: '',
    endTime: '',
    taskRemark: '',
    from: 'pc',
    taskDate:''
  }
  rules: any = {
    workerId: [{ required: true, message: '请填写工人姓名', trigger: 'change' }],
    beginTime: [{ required: true, message: '请填写开始时间', trigger: 'change' }],
    endTime: [{ required: true, message: '请填写结束时间', trigger: 'change' }]
  }

  open() {
    this.visible = true
  }

  save() {
    this.$refs.form.validate((valid: any) => {
      if (valid) {
        this.formData.beginTime = `${this.formData.beginTime}:00`
        this.formData.endTime = `${this.formData.endTime}:00`
        this.formData.taskDate = dayjs(this.formData.taskDate).valueOf()
        
        this.onAddAttendance()
        //  this.$emit('add-attendance', { ...this.formData })
        // this.visible = false
      }
    })
  }

   onAddAttendance() {
     this.ADD_ATTENDANCE(this.formData).then(res=>{
       console.log(res)
    if (res) {
      this.close()
    } else {
      let name = this.formData.workerId
      this.clear()
      this.formData.workerId = name
    }
     })
  }
  clear() {
    this.$refs.form.resetFields()
  }
  close() {
    this.formData = {
      workerId: '',
      beginTime: '',
      endTime: '',
      workRemark: '',
      from: 'pc'
    }
    this.visible = false
  }
}
</script>

<style lang="scss" scoped>
.addwork {
  margin-top: 30px;
  .el-form-item {
    margin-bottom: 22px;
  }
  .footer {
    border-top: 1px solid #eee;
    padding: 10px 20px;
    background: #fefefe;
    text-align: center;
  }
}
.el-dialog__wrapper .el-dialog .el-dialog__body .el-input__inner{
  cursor: pointer!important;
}
</style>
